package com.wangzhixuan.controller;

import com.wangzhixuan.constant.SystemConstant;
import com.wangzhixuan.db.bean.Customer;
import com.wangzhixuan.db.bean.Doctor;
import com.wangzhixuan.service.CustomerService;
import com.wangzhixuan.vo.JsonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/")
public class CustomerController extends BaseController{
    @Autowired
    private CustomerService customerService;

    
    @ResponseBody
    @RequestMapping(value = "/updateCustomer.json", method = RequestMethod.POST)
    public JsonVo<Customer> updateCustomer(HttpServletRequest request, HttpServletResponse response,
                                   String name, int sex, String position, String cellphone,String clinic, String address, String introduction) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JsonVo<Customer> vo=new JsonVo<>();
        Customer customer=getCurrentCustomer(request);
        customer.setName(name);
        customer.setSex(sex);
        customer.setCellphone(cellphone);
        customer.setPosition(position);
        customer.setClinic(clinic);
        customer.setAddress(address);
        customer.setIntroduction(introduction);
        customerService.updateCustomer(customer);

        HttpSession session = request.getSession();
        customer.setPassword("");
        session.setAttribute(SystemConstant.SESSION_CUSTOMER_KEY, customer);

        vo.setResult(true);
        vo.setT(customer);
        return vo;
    }


    @ResponseBody
    @RequestMapping(value = "/updateCustomerTwo.json", method = RequestMethod.POST)
    public JsonVo<Customer> updateCustomerTwo(HttpServletRequest request, HttpServletResponse response,
                                           String cellphone,String clinic, String address, String introduction) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JsonVo<Customer> vo=new JsonVo<>();
        Customer customer=getCurrentCustomer(request);
        customer.setCellphone(cellphone);
        customer.setClinic(clinic);
        customer.setAddress(address);
        customer.setIntroduction(introduction);
        customerService.updateCustomer(customer);

        HttpSession session = request.getSession();
        customer.setPassword("");
        session.setAttribute(SystemConstant.SESSION_CUSTOMER_KEY, customer);

        vo.setResult(true);
        vo.setT(customer);
        return vo;
    }

}
